import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
import qs from 'qs'

import {
	createApp
} from 'vue'
import App from './App.vue'
import router from './router'

import {
	getCurDate,
	setSessionStorage,
	getSessionStorage,
	removeSessionStorage,
	setLocalStorage,
	getLocalStorage,
	removeLocalStorage
} from './common.js'

//设置axios的基础url部分
// axios.defaults.baseURL = 'http://localhost:8080/elm/';
axios.defaults.baseURL = 'https://elm.seele.love/elm/';
//设置请求拦截器,添加api
axios.interceptors.request.use(
  (config) => {
    // 在发送请求之前添加信息
    // 比如 config.headers.Authorization = `Bearer ${token}`;
	let apiKey = getSessionStorage('apiKey');
	if (apiKey == '' || apiKey == null || apiKey == 'null' || apiKey == undefined) {
		config.headers['apiKey'] = '';
	} else {
		config.headers['apiKey'] = apiKey;
	}
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

router.beforeEach(function(to, from, next) {
	let user = sessionStorage.getItem('user');
	//除了登录、注册、首页之外，都需要判断是否登录
	if (!(to.path == '/' || to.path == '/index' ||
			to.path == '/login' || to.path == '/register')) {
		if (user == null) {
			router.push('/login');
			//更新到vue3后这句话会导致页面被强制加载到首页，所以删去
			// location.reload();
		}
	}
	let business = sessionStorage.getItem('business');
	//增加对商家是否登录的判断
	if (to.path == '/businessManagement') {
		if (business == null) {
			router.push('/businessLogin');
			// location.reload();
		}
	}
	let student = sessionStorage.getItem('student')
	
	if (to.path == '/tJUBusinessList') {
		if (student == null) {
			router.push('/studentLogin');
			// location.reload();
		}
	}
	next();
});

const app = createApp(App)

app.config.productionTip = false

app.config.globalProperties.$axios = axios;
app.config.globalProperties.$qs = qs;
app.config.globalProperties.$getCurDate = getCurDate;
app.config.globalProperties.$setSessionStorage = setSessionStorage;
app.config.globalProperties.$getSessionStorage = getSessionStorage;
app.config.globalProperties.$removeSessionStorage = removeSessionStorage;
app.config.globalProperties.$setLocalStorage = setLocalStorage;
app.config.globalProperties.$getLocalStorage = getLocalStorage;
app.config.globalProperties.$removeLocalStorage = removeLocalStorage;

app.use(router)
app.mount('#app')